package com.sinochem.yunlian.ship.base.rest.app;


import com.sinochem.yunlian.ship.api.vo.uc.UcUserInfo;
import com.sinochem.yunlian.ship.base.biz.BsMaterialCategoryFavBiz;
import com.sinochem.yunlian.ship.base.entity.BsMaterialCategoryFav;
import com.sinochem.yunlian.ship.common.annotation.Subject;
import com.sinochem.yunlian.ship.base.vo.request.ReqMaterialCategoryFavVo;
import com.sinochem.yunlian.ship.base.vo.response.ResMaterialCategoryFavVo;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("m/materialCategoryFav")
public class MBsMaterialCategoryFavController extends BaseController<BsMaterialCategoryFavBiz,BsMaterialCategoryFav> {

    @RequestMapping(value = "/listMaterialInfo", method = RequestMethod.POST)
    public RestResponse listMaterialInfo(@Subject UcUserInfo curUser)  {
        //检查是否所属公司
        Long companyId = curUser.getCompanyId();
        if (companyId == 0) {
            return RestResponse.response(4000, "公司不存在");
        }
        List<BsMaterialCategoryFav> list = baseBiz.selectMaterialCategroyFavListByUserId(companyId);
        List<ResMaterialCategoryFavVo> result = new ArrayList<>();
        if (list != null || list.size() > 0){
            for (BsMaterialCategoryFav fav:list){
                ResMaterialCategoryFavVo vo = new ResMaterialCategoryFavVo();
                vo.setId(fav.getId());
                vo.setMaterialCategoryId(fav.getMaterialCategoryId());
                vo.setMaterialCategoryName(fav.getMaterialCategoryName());
                vo.setDensity(fav.getDensity());
                vo.setTemperature(fav.getTemperature());
                result.add(vo);
            }
        }
        return RestResponse.success(result);
    }

    @RequestMapping(value = "/addMaterialInfo", method = RequestMethod.POST)
    public RestResponse addMaterialInfo(@Subject UcUserInfo curUser, @RequestBody ReqMaterialCategoryFavVo vo)  {
        //检查是否所属公司
        Long companyId = curUser.getCompanyId();
        if (companyId == 0) {
            return RestResponse.response(4000, "公司不存在");
        }

        String msg = baseBiz.AddMaterialCategroyFav(curUser.getId(), companyId, vo);
        if (StringUtils.isBlank(msg)) {
            return RestResponse.success();
        }
        else{
            return RestResponse.response(4000,msg);
        }
    }

    @RequestMapping(value = "/deleteMaterialInfo", method = RequestMethod.POST)
    public RestResponse deleteMaterialInfo(@Subject UcUserInfo curUser, @RequestBody Map<String, Object> inMap)  {
        //检查是否所属公司
        Long companyId = curUser.getCompanyId();
        if (companyId == 0) {
            return RestResponse.response(4000, "公司不存在");
        }

        if (!inMap.containsKey("materialCategoryId"))
        {
            return RestResponse.response(4000,"参数错误");
        }
        long materialCategoryId = Long.parseLong(inMap.get("materialCategoryId").toString());

        String msg = baseBiz.DeleteMaterialCategroyFav(curUser.getId(), companyId, materialCategoryId);
        if (StringUtils.isBlank(msg)) {
            return RestResponse.success();
        }
        else{
            return RestResponse.response(4000,msg);
        }
    }

    @RequestMapping(value = "/updateMaterialInfo", method = RequestMethod.POST)
    public RestResponse updateMaterialInfo(@Subject UcUserInfo curUser, @RequestBody ReqMaterialCategoryFavVo vo)  {
        //检查是否所属公司
        Long companyId = curUser.getCompanyId();
        if (companyId == 0) {
            return RestResponse.response(4000, "公司不存在");
        }

        String msg =  baseBiz.updateMaterialCategroyFav(companyId, vo);
        if (StringUtils.isBlank(msg)) {
            return RestResponse.success();
        }
        else{
            return RestResponse.response(4000,msg);
        }
    }
}